<?php

namespace App\Services\Home;

use App\Models\Album;
use App\Models\Apartment;
use App\Models\Attribute;
use App\Models\AttributeProperty;
use App\Models\Home\Property;

class PropertyService
{
    // 获取特色标识
    public function getUniqueid($id)
    {
        if(is_numeric($id) && $id > 0){
            $uniqueid = Attribute::findOrFail($id);
            return $uniqueid['uniqueid'];
        }
    }

    public function data($request)
    {
        $res = Property::valid($request->input('sort'))
            ->filter($request->all())
            ->paginateFilter($request->input('pagesize',10));

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'total' => $res['total'],
            'data' => $res
        ];
        return response()->json($data);
    }

    /**
     * @param $id
     * @return mixed
     * 楼盘详情
     */
    public function getDetailById($id)
    {
        return Property::with(['album','apartments','articles'])->findOrFail($id);
    }

    public function getPropertyApartment($id)
    {
        $data = Apartment::with('attribute')->where('property_id',$id)->where('status','T')->get()->groupBy('room');
        return $data;
    }

    public function getPropertyAlbum($id)
    {
        return Album::with('category')->where('property_id',$id)->where('status','T')->get();
    }

    /**
     * @param $rec
     * @param int $num
     * @return mixed
     * 获取推荐楼盘信息
     */
    public function getListByRecommend($rec, $num=8){
        $data = Property::where('recommend','like','%'.$rec.'%')->take($num)->get();
        return $data;
    }

    // 属性
    public function getPropertyByAttrId($ids,$type='property')
    {
        return AttributeProperty::where('type',$type)->whereIn('attribute_id',$ids)->get(['property_id'])->toArray();
    }
}